import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12


Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    //1. 信号没有返回值
    //2. 数据类型使用string, int等
    signal  changeDefault(string data)


    //接收c++信号
    //方式1
//    Connections {
//        target: dataManager
//        //这里名称规则 on + c++代码中信号的名称  ， c++代码中信号的名称 首字母大写
//        onDataSend: {
//            console.log("Received data from C++:", name , val)
//            recText.text = name + val
//        }
//    }

    //方式2

    Component.onCompleted: {
        connectToDataSend()
    }

    function connectToDataSend() {
        //通过connect绑定处理信号
        dataManager.dataSend.connect(function(name, val) {
            console.log("Received data from C++:", name, val)
            recText.text = name + " " + val.toString()
        })
    }


    Row {
        x: 0
        y: 0
        width: parent.width
        height: 80
        TextEdit {
            id: inputEdit
            width: parent.width - 80
            height: parent.height
            text: qsTr("默认值")
        }


        Button{
            id: writeBtn
            width: 80
            height: 20
            text: qsTr("修改c++")
            onClicked: {
                dataManager.name = inputEdit.text
            }
        }
    }

    Row {
        x: 0
        y: 80
        width: parent.width
        height: 50
        Text {
            id: nameText
            width: parent.width - 80
            height: parent.height
            text: qsTr("nameText")
        }

        Button{
            id: readBtn
            width: 80
            height: 20
            text: qsTr("读取c++")
            onClicked: {
                var test = dataManager.readName()
                console.log("test: ", test)
                nameText.text =test
            }
        }
    }

    Row {
        x: 0
        y: 160
        width: parent.width
        height: 50

        Button{
            id: sendBtn
            width: 80
            height: 20
            text: qsTr("发送信号")
            onClicked: {
                changeDefault(inputEdit.text)
            }
        }
    }

    Row {
        x: 0
        y: 210
        width: parent.width
        height: 50

        Text {
            width: 150
            text: qsTr("接收信号数据：")
        }
        Text {
            id: recText
            width: parent.width - 100
        }
    }
}
